<?php

namespace Admin\Controller;

use Think\Controller;

class IndexController extends CommonController
{
    public function index()
    {

        //超级管理员登录
        if (session(C('ADMIN_AUTH_KEY'))) {
            $node = D('node')->where("level = 2")->order('sort')->relation(true)->select();
        } else {     //其他管理员登录
            //取出权限节点
            $node = D('node')->where("level = 2")->order('sort')->relation(true)->select();
            //取出当前登录用户的所有模块权限(英文名称)和操作标识(ID)
            $module = '';
            $node_id = '';
            $accessList = $_SESSION['_ACCESS_LIST'];

            foreach ($accessList as $key => $value) {
                foreach ($value as $key1 => $value1) {
                    $module = $module . ',' . $key1;
                    foreach ($value1 as $key2 => $value2) {
                        $node_id = $node_id . ',' . $value2;
                    }
                }
            }

            //去除没有权限的节点
            foreach ($node as $key => $value) {
                if (!in_array(strtoupper($value['name']), explode(',', $module))) {
                    unset($node[$key]);
                } else {
                    //模块存在，比较里面的操作
                    foreach ($value['node'] as $key1 => $value1) {
                        if (!in_array($value1['id'], explode(',', $node_id))) {
                            unset($node[$key]['node'][$key1]);   //一层一层下来，删除此操作
                        }
                    }
                }
            }
        }

        $this->assign('node', $node);
        $this->display();
    }

}
